package com.xherorl.netty;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

/**
 * @author ：Xherorl
 * @date ：Created in 2023/8/20 20:17
 * @description：
 */
public class MyWatcher implements Watcher {


    // 实际开发中只需要关心自己的watcher事件即可


    @Override
    public void process(WatchedEvent watchedEvent) {  // watchedEvent: 事件
        // 判断事件类型，是否是连接类型的事件
        if (watchedEvent.getType() == Event.EventType.None){
            if (watchedEvent.getState() == Event.KeeperState.SyncConnected){
                System.out.println("zookeeper连接成功");
            }else if (watchedEvent.getState() == Event.KeeperState.AuthFailed){
                System.out.println("zookeeper认证失败");
            } else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
                System.out.println("zookeeper断开连接");
            }
        }else if (watchedEvent.getType() == Event.EventType.NodeCreated){
            System.out.println(watchedEvent.getPath() + "节点被创建");
        }else if (watchedEvent.getType() == Event.EventType.NodeDeleted){
            System.out.println(watchedEvent.getPath() + "节点被删除");
        }else if (watchedEvent.getType() == Event.EventType.NodeDataChanged){
            System.out.println(watchedEvent.getPath() + "节点数据被更改");
        }else if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged){
            System.out.println(watchedEvent.getPath() + "子节点发生变化");
        }
    }
}
